package com.github.taomus.fig.antlr4.test

import cool.taomu.software.fig.antlr4.CalcBaseListener
import cool.taomu.software.fig.antlr4.CalcLexer
import cool.taomu.software.fig.antlr4.CalcParser
import cool.taomu.software.fig.antlr4.CalcParser.IntContext
import cool.taomu.software.fig.antlr4.entry.Antlr4Entry
import cool.taomu.software.fig.asm.aspect2.AspectHandle
import cool.taomu.software.fig.asm.aspect2.annotation.Around
import cool.taomu.software.fig.asm.aspect2.annotation.Aspect
import java.util.Stack
import org.junit.Test

import static org.junit.Assert.assertEquals

class CalcTest {
	@Aspect
	static class Clistener {
		public static Stack<Integer> stack = new Stack<Integer>();

		@Around(value=CalcBaseListener)
		def calc(AspectHandle aspect) {
			var arg = aspect.arguments.get(0);
			switch (aspect.methodName) {
				case "exitInt": {
					var _int = arg as IntContext;
					stack.add(Integer.parseInt(_int.INT().getText()))
				}
				case "exitAdd": {
					var r = stack.pop();
					var l = stack.pop();
					stack.add(l + r);
				}
				case "exitSub": {
					var int r = stack.pop();
					var int l = stack.pop();
					stack.add(l - r);
				}
			}
		}
	}

	@Test
	def void test1() {
		Antlr4Entry.entry(CalcLexer,CalcParser,CalcBaseListener,Clistener,"s","1+2")
		var r = Clistener.stack.pop();
		assertEquals(r,3);
	}
}
